package com.smasher.widget.base

import android.content.Context
import android.graphics.Color
import android.util.TypedValue
import android.view.View
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

open class BaseIndicatorAdapter<T> : CommonNavigatorAdapter() {
    private var mList: ArrayList<T> = ArrayList()
    private var mIndicatorListener: IndicatorListener? = null
    private var colorNormal = Color.GRAY
    private var colorSelected = Color.BLACK
    private var textSize = 14f
    private var colorIndicator = Color.BLACK

    fun setData(data: List<T>) {
        mList.clear()
        mList.addAll(data)
    }

    fun setColorNormal(colorNormal: Int) {
        this.colorNormal = colorNormal
    }

    fun getColorNormal(): Int {
        return colorNormal
    }

    fun setColorSelected(colorSelected: Int) {
        this.colorSelected = colorSelected
    }

    fun getColorSelected(): Int {
        return colorSelected
    }

    fun setTextSizeActual(textSize: Float) {
        this.textSize = textSize
    }

    fun getTextSizeActual(): Float {
        return textSize
    }

    fun setColorIndicator(colorIndicator: Int) {
        this.colorIndicator = colorIndicator
    }

    fun getColorIndicator(): Int {
        return colorIndicator
    }

    override fun getCount(): Int {
        return mList.size
    }

    override fun getTitleView(context: Context, index: Int): IPagerTitleView {
        val text = SimplePagerTitleView(context)
        text.normalColor = colorNormal
        text.selectedColor = colorSelected
        val title = getTitleFromItem(index)
        text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize)
        text.text = title
        text.tag = index
        text.setOnClickListener(onClickListener)
        return text
    }

    override fun getIndicator(context: Context): IPagerIndicator {
        val indicator = LinePagerIndicator(context)
        indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
        indicator.setColors(colorIndicator)
        return indicator
    }

    private val onClickListener = View.OnClickListener { v ->
        val index = v.tag as Int
        mIndicatorListener?.onItemClick(index, v)
    }

    open fun setOnItemClickListener(listener: IndicatorListener) {
        mIndicatorListener = listener
    }

    open fun getTitleFromItem(index: Int): String {
        mList[index]
        return "default"
    }

    protected fun getItem(i: Int): T? {
        return if (i in 0 until mList.size) {
            mList[i]
        } else {
            null
        }
    }

    interface IndicatorListener {
        fun onItemClick(dex: Int, view: View?)
    }
}